package com.j180.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.j180.bean.StudentBean;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class TestServlet extends HttpServlet {


    @Override
    public void init() throws ServletException {

        System.out.println("TestServlet====>>>被创建");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        System.out.println("TestServlet=====>>>>");

        String id = req.getParameter("studentId");
        String name = req.getParameter("studentName");
        StudentBean studentBean = new StudentBean(Integer.parseInt(id),name,"男");
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(studentBean);
        resp.getWriter().print(json);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        this.doPost(req,resp);
    }
}
